package com.zoke.dappstore.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.zoke.dappstore.R;
import com.zoke.dappstore.adapter.DmAdapter;
import com.zoke.dappstore.base.NebActivity;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 下载管理页面
 */
@ContentView(R.layout.activity_dm)
public class DmActivity extends NebActivity {

    private DmAdapter dmAdapter;
    private List<DownloadEntity> mList = new ArrayList<>();
    @ViewInject(R.id.recycler)
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.setTitle("下载管理");
        mTitleBar.setLeft(R.drawable.ic_back, null);
        dmAdapter = new DmAdapter(mList);
        dmAdapter.openLoadAnimation();
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(dmAdapter);
        Aria.download(this).register();
        List<DownloadEntity> list = Aria.download(this).getTaskList();
        if (list != null && list.size() != 0) {
            mList.addAll(list);
            dmAdapter.notifyDataSetChanged();
            dmAdapter.loadMoreEnd(false);
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        initBar(R.id.status, false);
    }


    @Download.onPre
    void onPre(DownloadTask task) {
        dmAdapter.setDone(task.getEntity());
        LogUtil.e(task.getTaskName() + ", " + task.getState());
    }

    @Download.onTaskStart
    void taskStart(DownloadTask task) {
        LogUtil.e(task.getTaskName() + ", " + task.getState());
        dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskResume
    void taskResume(DownloadTask task) {
        LogUtil.e(task.getTaskName() + ", " + task.getState());
        dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskStop
    void taskStop(DownloadTask task) {
        dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskCancel
    void taskCancel(DownloadTask task) {
       dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskFail
    void taskFail(DownloadTask task) {
        dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskComplete
    void taskComplete(DownloadTask task) {
        dmAdapter.setDone(task.getEntity());
    }

    @Download.onTaskRunning()
    void taskRunning(DownloadTask task) {
        dmAdapter.setProgress(task.getEntity());
    }

}
